package com.king.cms.util.page;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class ZoomImage {

	private int width = 160;
	private int height = 120;
	
	public ZoomImage(){	}
	public ZoomImage(int width , int height){
		this.width = width;
		this.height = height;		
	}
	
	
	
	public long createImage(InputStream ins, File file){		
		FileOutputStream fos = null;
		long size = 0;
		try {
			fos = new FileOutputStream(file);
			Image src = javax.imageio.ImageIO.read(ins);
			BufferedImage bImage = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
			bImage.getGraphics().drawImage(src, 0, 0, width, height, null);
			JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fos);
			encoder.encode(bImage);
			 
			byte[] data = new byte[1024];
			int len ;
			while((len=(ins.read(data,0,1024))) != -1){
				fos.write(data,0,len);	
				size += len;
			}
			fos.close();
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return size;
	}
	
	
	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
